package com.ljh.account.controller;

import com.ljh.account.dao.Account;
import com.ljh.account.dao.Detail;
import com.ljh.account.service.AccountService;
import com.ljh.account.utils.Response;
import com.ljh.account.utils.Upload;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/protected")
public class DetailController {
    @Autowired
    AccountService accountService;
    @GetMapping("/detail")
    public Response detail(@RequestParam long id){
        Account account  = accountService.findAccountById(id);
        // 使用 modelmapper 库将account的属性仿佛之到detail中
        ModelMapper modelMapper = new ModelMapper();
        Detail detail = modelMapper.map(account, Detail.class);
        return Response.success(detail);
    }
    @PutMapping("/detail")
    public Response detail(@RequestBody Account account){
        System.out.println(account.getAvatarUrl());
        accountService.updateAccount(account);
        return Response.success("用户信息更新成功！");
    }
    //    上传用户头像
    @PostMapping("/uploadavt")
    public Response uploadAvt(@RequestParam("file") MultipartFile file) {
        Object data = Upload.uploadImage(file,"avt");
        if(data != null){
            return Response.success(data);
        }else {
            return Response.error("头像上传失败");
        }
    }
}
